import { EPokerStatus } from "../../ConfigEnum"
import { Poker } from "../../GameScene/GameDB"

const PPINT_MAP = {
  '1': 'A',
  '2': '2',
  '3': '3',
  '4': '4',
  '5': '5',
  '6': '6',
  '7': '7',
  '8': '8',
  '9': '9',
  '10': '10',
  '11': 'J',
  '12': 'Q',
  '13': 'K',
}

const { ccclass, property } = cc._decorator

@ccclass
export default class UIPoker extends cc.Component {
  // nodes
  @property(cc.Sprite)
  bgSprite: cc.Sprite = null

  @property(cc.Label)
  pointLabel: cc.Label = null

  // 左侧小花色
  @property(cc.Sprite)
  smallSuitSprite: cc.Sprite = null

  @property(cc.Sprite)
  bigSuitSprite: cc.Sprite = null

  @property(cc.Sprite)
  cardBG: cc.Sprite = null

  // resources
  @property(cc.Color)
  redTextColor: cc.Color = cc.Color.RED // 红色

  @property(cc.Color)
  blackTextColor: cc.Color = cc.Color.BLACK // 黑色

  //前景色
  @property(cc.SpriteFrame)
  texFrontBG: cc.SpriteFrame = null


  @property(cc.SpriteFrame)
  texBackBG: cc.SpriteFrame = null


  @property(cc.SpriteFrame)
  texFaces: cc.SpriteFrame[] = []


  @property(cc.SpriteFrame)
  texSuitBig: cc.SpriteFrame[] = []


  @property(cc.SpriteFrame)
  texSuitSmall: cc.SpriteFrame[] = []

  public Init(poker: Poker) {
    this.pointLabel.string = `${PPINT_MAP[poker.point]}`
    this.pointLabel.node.color = poker.suit % 2 === 0 ? this.blackTextColor : this.redTextColor
    this.smallSuitSprite.spriteFrame = this.texSuitSmall[poker.suit]
    this.bigSuitSprite.spriteFrame = this.texSuitBig[poker.suit]

    if (poker.point >= 11) {
      this.bigSuitSprite.spriteFrame = this.texFaces[poker.point - 10 - 1]
    }
    this.setStatus(poker.status)
  }


  private setStatus(status: EPokerStatus) {
    if (status === EPokerStatus.CLOSE) {
      this.pointLabel.node.active = false
      this.smallSuitSprite.node.active = false
      this.bigSuitSprite.node.active = false
      this.bgSprite.spriteFrame = this.texBackBG
    } else if (status === EPokerStatus.OPEN) {
      this.pointLabel.node.active = true
      this.smallSuitSprite.node.active = true
      this.bigSuitSprite.node.active = true
      this.bgSprite.spriteFrame = this.texFrontBG
    }
  }
}